{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import os\n",
    "from tqdm import tqdm\n",
    "import lightgbm as lgb\n",
    "from sklearn.model_selection import StratifiedKFold\n",
    "from sklearn import metrics\n",
    "import warnings\n",
    "import matplotlib.pyplot as plt\n",
    "from collections import Counter\n",
    "\n",
    "pd.set_option('display.max_columns', 100)\n",
    "pd.set_option('display.max_rows', 200)\n",
    "warnings.filterwarnings('ignore')\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [],
   "source": [
    "result_path = 'results/'\n",
    "result_n = len(os.listdir(result_path))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['.ipynb_checkpoints',\n",
       " 'result_200208_0.8763.csv',\n",
       " 'res_lgb300per_20200205_161940-0.87829.csv',\n",
       " 'res_lgb300per_20200205_175908-0.87826.csv',\n",
       " 'res_lgb_mer200_per_20200208_011324-0.87029.csv',\n",
       " 'res_lgb_mer200_per_20200208_011324-0.87663.csv']"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.listdir(result_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_df(path):\n",
    "    df_1 = pd.read_csv(path+'result_200208_0.8763.csv', header=None)\n",
    "    df_1.columns = ['ship','type']\n",
    "    df_1.sort_values(by ='ship',inplace =True)\n",
    "    \n",
    "    n = len(os.listdir(path))\n",
    "    for i in range(n-2):\n",
    "        re_dir = os.listdir(path)[i+2]  ### 参数调整\n",
    "        print(re_dir)\n",
    "        df = pd.read_csv(path+'%s' %re_dir , header=None)\n",
    "        df.columns = ['ship','type_%s' %i]\n",
    "        df.sort_values(by ='ship',inplace =True)\n",
    "        df_1 = df_1.merge(df, how ='left',on ='ship')\n",
    "        \n",
    "    return df_1 \n",
    "\n",
    "def get_result(df):\n",
    "    lst = []\n",
    "    for i in range(df.shape[0]):\n",
    "        lst.append(Counter(df.iloc[i,]).most_common(1)[0][0])\n",
    "    df['most_common'] = lst\n",
    "    \n",
    "    return df     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "res_lgb300per_20200205_161940-0.87829.csv\n",
      "res_lgb300per_20200205_175908-0.87826.csv\n",
      "res_lgb_mer200_per_20200208_011324-0.87029.csv\n",
      "res_lgb_mer200_per_20200208_011324-0.87663.csv\n"
     ]
    }
   ],
   "source": [
    "result_all = get_df(result_path)\n",
    "result_all_1 = get_result(result_all)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "result_all_1[['ship','most_common']].to_csv('result_200210_voting_01.csv', index=None, header=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>7000</td>\n",
       "      <td>围网</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>7001</td>\n",
       "      <td>拖网</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7002</td>\n",
       "      <td>围网</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>7003</td>\n",
       "      <td>拖网</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>7004</td>\n",
       "      <td>围网</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995</th>\n",
       "      <td>8995</td>\n",
       "      <td>围网</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996</th>\n",
       "      <td>8996</td>\n",
       "      <td>围网</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997</th>\n",
       "      <td>8997</td>\n",
       "      <td>围网</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998</th>\n",
       "      <td>8998</td>\n",
       "      <td>拖网</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1999</th>\n",
       "      <td>8999</td>\n",
       "      <td>围网</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2000 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         0   1\n",
       "0     7000  围网\n",
       "1     7001  拖网\n",
       "2     7002  围网\n",
       "3     7003  拖网\n",
       "4     7004  围网\n",
       "...    ...  ..\n",
       "1995  8995  围网\n",
       "1996  8996  围网\n",
       "1997  8997  围网\n",
       "1998  8998  拖网\n",
       "1999  8999  围网\n",
       "\n",
       "[2000 rows x 2 columns]"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv('result_200210_voting_01.csv', header=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
